using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.AspNetCore.Mvc;
using TenantManagementService.Tenants;

namespace TenantManagementService.Controllers
{
    /// <summary>
    /// 租户管理控制器
    /// </summary>
    [ApiController]
    [Route("api/tenants")]
    public class TenantController : AbpControllerBase
    {
        private readonly ITenantAppService _tenantAppService;

        public TenantController(ITenantAppService tenantAppService)
        {
            _tenantAppService = tenantAppService;
        }

        /// <summary>
        /// 获取租户列表
        /// </summary>
        /// <param name="input">分页查询参数</param>
        /// <returns>租户列表</returns>
        [HttpGet]
        public async Task<PagedResultDto<TenantDto>> GetListAsync([FromQuery] PagedAndSortedResultRequestDto input)
        {
            return await _tenantAppService.GetListAsync(input);
        }

        /// <summary>
        /// 根据ID获取租户信息
        /// </summary>
        /// <param name="id">租户ID</param>
        /// <returns>租户信息</returns>
        [HttpGet("{id}")]
        public async Task<TenantDto> GetAsync(Guid id)
        {
            return await _tenantAppService.GetAsync(id);
        }

        /// <summary>
        /// 根据编码获取租户信息
        /// </summary>
        /// <param name="code">租户编码</param>
        /// <returns>租户信息</returns>
        [HttpGet("by-code/{code}")]
        public async Task<TenantDto> GetByCodeAsync(string code)
        {
            return await _tenantAppService.GetByCodeAsync(code);
        }

        /// <summary>
        /// 创建新租户
        /// </summary>
        /// <param name="input">创建租户参数</param>
        /// <returns>创建的租户信息</returns>
        [HttpPost]
        public async Task<TenantDto> CreateAsync([FromBody] CreateTenantDto input)
        {
            return await _tenantAppService.CreateAsync(input);
        }

        /// <summary>
        /// 更新租户信息
        /// </summary>
        /// <param name="id">租户ID</param>
        /// <param name="input">更新租户参数</param>
        /// <returns>更新后的租户信息</returns>
        [HttpPut("{id}")]
        public async Task<TenantDto> UpdateAsync(Guid id, [FromBody] UpdateTenantDto input)
        {
            return await _tenantAppService.UpdateAsync(id, input);
        }

        /// <summary>
        /// 删除租户
        /// </summary>
        /// <param name="id">租户ID</param>
        [HttpDelete("{id}")]
        public async Task DeleteAsync(Guid id)
        {
            await _tenantAppService.DeleteAsync(id);
        }

        /// <summary>
        /// 激活租户
        /// </summary>
        /// <param name="id">租户ID</param>
        /// <returns>激活后的租户信息</returns>
        [HttpPost("{id}/activate")]
        public async Task<TenantDto> ActivateAsync(Guid id)
        {
            return await _tenantAppService.ActivateAsync(id);
        }

        /// <summary>
        /// 暂停租户
        /// </summary>
        /// <param name="id">租户ID</param>
        /// <param name="reason">暂停原因</param>
        /// <returns>暂停后的租户信息</returns>
        [HttpPost("{id}/suspend")]
        public async Task<TenantDto> SuspendAsync(Guid id, [FromQuery] string? reason = null)
        {
            return await _tenantAppService.SuspendAsync(id, reason);
        }

        /// <summary>
        /// 禁用租户
        /// </summary>
        /// <param name="id">租户ID</param>
        /// <param name="reason">禁用原因</param>
        /// <returns>禁用后的租户信息</returns>
        [HttpPost("{id}/disable")]
        public async Task<TenantDto> DisableAsync(Guid id, [FromQuery] string? reason = null)
        {
            return await _tenantAppService.DisableAsync(id, reason);
        }

        /// <summary>
        /// 启用租户
        /// </summary>
        /// <param name="id">租户ID</param>
        /// <returns>启用后的租户信息</returns>
        [HttpPost("{id}/enable")]
        public async Task<TenantDto> EnableAsync(Guid id)
        {
            return await _tenantAppService.EnableAsync(id);
        }

        /// <summary>
        /// 续期租户订阅
        /// </summary>
        /// <param name="id">租户ID</param>
        /// <param name="input">续期参数</param>
        /// <returns>续期后的租户信息</returns>
        [HttpPost("{id}/extend-subscription")]
        public async Task<TenantDto> ExtendSubscriptionAsync(Guid id, [FromBody] ExtendSubscriptionDto input)
        {
            return await _tenantAppService.ExtendSubscriptionAsync(id, input);
        }
    }
}